package com.edu.boot301demo.controller;

import com.edu.boot301demo.bean.Employee;
import com.edu.boot301demo.common.R;
import com.edu.boot301demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
//@CrossOrigin // 允许跨域访问
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/employees")
    public R<List<Employee>> getEmployees() {
        List<Employee> employees = employeeService.list();
        return R.success(employees);
    }

    @GetMapping("/employee/{id}")
    public R<Employee> getEmployeeById(@PathVariable("id") Integer id) {
//        Employee redisEmployee = (Employee) redisTemplate.opsForValue().get("employee:" + id);
//        System.out.println(redisEmployee);
//        if (redisEmployee != null) {
//            redisEmployee.setLastName(redisEmployee.getLastName() + " (Redis)");
//            return R.success(redisEmployee);
//        }
        Employee employee = employeeService.getById(id);
//        if (employee != null)
//            redisTemplate.opsForValue().set("employee:" + id, employee, Duration.ofSeconds(20));
//        else
//            throw new RuntimeException("未找到员工");
        return R.success(employee);
    }
}
